
#import "HomeViewController.h"
#import "HomeLayout.h"
#import "HomeCollectionnViewCell.h"
#import "HomeSecondViewController.h"
#import "SetupTableViewController.h"

@interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"设置.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction:)];
    
    
    HomeLayout *layout = [[HomeLayout alloc] init];
    
    //创建UICollectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    
    //添加到父视图
    [self.view addSubview:collectionView];
    
    //遵守协议，设置代理
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    //注册标记
    [collectionView registerNib:[UINib nibWithNibName:@"HomeCollectionnViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"HomeCollectionnViewCell"];
    
    
}

#pragma mark --- 实现方法 ---------- UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 20;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //1.创建cell
    HomeCollectionnViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeCollectionnViewCell" forIndexPath:indexPath];
    cell.mediumLogoImageView.image = [UIImage imageNamed:@"2.jpg"];
    
    
    return cell;
}

#pragma mark --- 实现方法 ---------- UICollectionViewDelegate
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark 点击item 的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击item执行的方法");
    HomeSecondViewController *homeSecondVC = [[HomeSecondViewController alloc] init];
    [self.navigationController pushViewController:homeSecondVC animated:YES];
    
}


#pragma mark  将要显示cell
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    CAKeyframeAnimation *key = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    key.duration = 0.5;
    id b1 = @(1.0);
    id b2 = @(0.6);
    id b3 = @(1.0);
    key.values = @[b1,b2,b3];
    [cell.layer addAnimation:key forKey:@"key"];
}

#pragma mark 设置按钮方法
-(void)rightBarButtonItemAction:(UIBarButtonItem *)sender
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Setup" bundle:[NSBundle mainBundle]];
    SetupTableViewController *setupVC = board .instantiateInitialViewController;
    [self.navigationController pushViewController:setupVC animated:YES];
    
}






@end
